package com.hzjh.h5mall.ui.login

import android.graphics.Paint
import com.alibaba.android.arouter.facade.annotation.Route
import com.hzjh.base.ext.R
import com.hzjh.base.ext.data.Configs
import com.hzjh.base.ext.frame.JHBaseActivity
import com.hzjh.base.ext.route.RouteUrl
import com.hzjh.base.router.ext.navigation
import com.hzjh.base.router.ext.navigationTo
import com.hzjh.base.style.weight.ToastKt.toastCenter
import com.hzjh.base.utils.function.isEmail
import com.hzjh.base.utils.other.CommUtils
import com.hzjh.base.utils.other.KeyboardUtils
import com.hzjh.h5mall.BuildConfig
import com.hzjh.h5mall.databinding.ActivityLoginBinding
import com.hzjh.h5mall.model.login.LoginVM

@Route(path = RouteUrl.LOGIN_ACTIVITY)
class LoginActivity : JHBaseActivity<ActivityLoginBinding, LoginVM>() {
    private var isAgree = false
    override fun initData() {

    }

    override fun initView() {
        viewBinding.btnLogin.paint.flags = Paint.UNDERLINE_TEXT_FLAG; //下划线
        viewBinding.btnLogin.paint.isAntiAlias = true;//抗锯齿

        viewBinding.btnRegister.paint.flags = Paint.UNDERLINE_TEXT_FLAG; //下划线
        viewBinding.btnRegister.paint.isAntiAlias = true;//抗锯齿

        viewBinding.tvForget.paint.flags = Paint.UNDERLINE_TEXT_FLAG; //下划线
        viewBinding.tvForget.paint.isAntiAlias = true;//抗锯齿

        viewBinding.inputEmail.setTextChangeListener {
            changeLoginStatus()
        }
        viewBinding.inputPassword.setTextChangeListener {
            changeLoginStatus()
        }
        viewBinding.ivCheckAgree.setOnClickListener {
            changeAgreeStatus()
        }
        viewBinding.tvCheckAgree.setOnClickListener {
            changeAgreeStatus()
        }

        viewBinding.btnLogin.setOnClickListener{
            KeyboardUtils.hideSoftkeyboard(this)
            //登录
            if(!isAgree){
                toastCenter(CommUtils.getString(R.string.jh_agree_policy_toast))
                return@setOnClickListener
            }
            val email = viewBinding.inputEmail.text.toString()
            if(!email.isEmail()){
                toastCenter(CommUtils.getString(R.string.jh_email_check_toast))
            }
            val password = viewBinding.inputPassword.text.toString()
            viewModel.reqEmailLogin(email,password)
        }
        viewBinding.btnRegister.setOnClickListener {
            //注册
            navigation(RouteUrl.REGISTER_ACTIVITY)
        }
        viewBinding.tvUserAgreement.setOnClickListener {
            //用户协议
            navigationTo(RouteUrl.WEB_ACTIVITY){
                withString(RouteUrl.PARAMS_WEB_URL,BuildConfig.h5Url+Configs.USER_AGREEMENT_URL)
                withBoolean(RouteUrl.PARAMS_WEB_BAR_VISIBILITY,true)
                withBoolean(RouteUrl.PARAMS_WEB_TITLE_BAR_VISIBILITY,true)
            }
        }
        viewBinding.tvForget.setOnClickListener {
            //忘记密码
            navigation(RouteUrl.FORGOT_ACTIVITY)
        }
    }

    private fun changeLoginStatus(){
        viewBinding.btnLogin.isEnabled=!viewBinding.inputEmail.text.isNullOrBlank()&&!viewBinding.inputPassword.text.isNullOrBlank()
    }
    private fun changeAgreeStatus(){
        isAgree = !isAgree
        if (isAgree) {
            viewBinding.ivCheckAgree.setImageResource(R.drawable.ic_checkbox_true)
        } else {
            viewBinding.ivCheckAgree.setImageResource(R.drawable.ic_checkbox_false)
        }
    }

    override fun initViewBinding(): ActivityLoginBinding {
        return ActivityLoginBinding.inflate(layoutInflater)
    }
}